package com.daoImpl;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.dao.GoodsDao;
import com.entity.Goods;

@Repository
public class GoodsDaoImpl implements GoodsDao{
	
	@Autowired     
	private SessionFactory sessionFactory;

	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public void delete(int id) {
	
		Goods good = (Goods)sessionFactory.getCurrentSession().get(Goods.class,id);
        sessionFactory.getCurrentSession().delete(good);
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRED,readOnly=true)
	public Object findByld(int id) {
		
		Goods good=sessionFactory.getCurrentSession().get(Goods.class, id);
        return good;
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRED,readOnly=true)
	public void insert(Object o) {
		
		sessionFactory.getCurrentSession().save(o);
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public void update(Object o) {
		 sessionFactory.getCurrentSession().update(o);
		
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRED,readOnly=true)
	public List findByName(String name) {
		List list = sessionFactory.getCurrentSession().createQuery("from Goods g where g.name = :name").setParameter("name", name).list(); 
		return list;
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRED,readOnly=true)
	public List findByCategory(String category) {
		List list = sessionFactory.getCurrentSession().createQuery("from Goods g where g.category = :category").setParameter("category", category).list();       
        return list;
	}

	

}
